package com.example.jikeyoujikeyou.office_sz.view.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.jikeyoujikeyou.office_sz.R;
import com.example.jikeyoujikeyou.office_sz.model.HttpModel;
import com.example.jikeyoujikeyou.office_sz.model.Staff;
import com.example.jikeyoujikeyou.office_sz.service.INetCallBack;
import com.example.jikeyoujikeyou.office_sz.service.StaffService;
import com.example.jikeyoujikeyou.office_sz.view.activity.forget.ForgetActivity;
import com.example.jikeyoujikeyou.office_sz.view.activity.main.MainActivity;
import com.example.jikeyoujikeyou.office_sz.view.activity.register.RegisterActivity;
import com.example.jikeyoujikeyou.office_sz.view.base.BaseActivity;

public class LoginActivity extends BaseActivity {

    private TextView tv_forget, tv_register;
    private EditText username,password;
    private Button login;
    private int try_time = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*1.设置隐藏标题栏*/
        getTitleBar().setVisibility(View.GONE);
        setContentView(R.layout.activity_login);
        init();
    }

    private void init(){
        tv_forget = (TextView) findViewById(R.id.tv_forget);
        tv_register = (TextView) findViewById(R.id.tv_register);

        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        login = (Button) findViewById(R.id.login);
        addListener();
    }

    private void addListener() {
        tv_forget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginActivity.this,ForgetActivity.class));
            }
        });

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username_str = username.getText().toString();
                String password_str = password.getText().toString();

                if (username_str != null && password_str != null && !username_str.trim().equals("") && !password_str.trim().equals("") ) {

                    Staff staff = new Staff();
                    staff.setPhone(username_str);
                    staff.setPassword(password_str);

                    System.out.println(staff);

                    StaffService.login(staff, new INetCallBack() {
                        @Override
                        public void onRequstSuccess(HttpModel httpModel) {
                            startActivity(new Intent(LoginActivity.this,MainActivity.class));
                        }

                        @Override
                        public void onRequestFail(String msg) {
                            fail(msg);
                        }

                        @Override
                        public void onRequestFail() {
                            fail("账号或密码错误，请重新输入。" );
                        }
                    });
                } else {
                    fail("账号或密码错误，请重新输入。" );
                }
            }
        });

        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
            }
        });
    }

}
